package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.kt.uokos.yimo.dao.SearchHistory;
import com.kt.uokos.yimo.example.SearchHistoryExample;
import com.kt.uokos.yimo.mapper.SearchHistoryMapper;
import com.kt.uokos.yimo.service.SearchHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class SearchHistoryServiceImpl implements SearchHistoryService {
    @Resource
    private SearchHistoryMapper searchHistoryMapper;

    @Override
    public List<SearchHistory> querySelective(String userId, String keyword, Integer page, Integer size, String sort, String order) {

        SearchHistoryExample example = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page,size);
        return searchHistoryMapper.selectByExample(example);
    }

    @Override
    public void add(SearchHistory searchHistory) {
        searchHistory.setAddTime(LocalDateTime.now());
        searchHistory.setUpdateTime(LocalDateTime.now());
        searchHistoryMapper.insert(searchHistory);
    }

    @Override
    public List<SearchHistory> queryByUid(Integer uId) {
        SearchHistoryExample example = new SearchHistoryExample();
        example.or().andUserIdEqualTo(uId).andDeletedEqualTo(false);
        return searchHistoryMapper.selectByExample(example);
    }
}
